package com.ycsj.goldmedalnewconcept.main.activity;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.just.agentweb.AgentWeb;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.goldmedalnewconcept.R;

import butterknife.BindView;
import butterknife.OnClick;

public class AgreementAndPrivacyActivity extends BaseActivity {

    @BindView(R.id.rl_content)
    RelativeLayout rlContent;

    //标题栏
    @BindView(R.id.tv_title)
    TextView tvTitle;

    private AgentWeb mAgentWeb;

    private String webUrl;

    public static void goActivity(Context context, String title) {
        Intent it = new Intent(context, AgreementAndPrivacyActivity.class);
        it.putExtra("title", title);
        context.startActivity(it);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_agreement_privacy;
    }

    @Override
    protected void init() {

        //设置标题
        setTitle();

        //加载webview
        initAndAddWebView();
    }

    //设置标题
    public void setTitle() {
        String title = getIntent().getStringExtra("title");
        tvTitle.setText(title);
        if (title.equals("用户协议")) {
            webUrl = "file:///android_asset/ServiceAgreement.html";
        } else if (title.equals("隐私政策")) {
            webUrl = "file:///android_asset/JpjProvacyPolicy.html";
        }
    }

    /**
     * 初始化WebView并添加到视图
     */
    private void initAndAddWebView() {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(rlContent, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go(webUrl);

    }


    //点击事件
    @OnClick({R.id.iv_back})
    public void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }
}
